﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aulac.Verb.Ems;
using System.Xml;
using Aulac.Global.Xml;
using Aulac.Rsc.Ems.Tags;
using Aulac.Rsc;
using Aulac.Global.Enums;
using Aulac.Client.Global.Interface;
using Aulac.Global.Common;
using System.Data;
using Aulac.Global.Data;

namespace Aulac.Client.Ems.WinForm.Stores
{
    partial class FormTranferMdnStore
    {
        private bool SaveTranferMdnStore(ref XmlDataDocument xmlData, FormModes pFormMode)
        {
            /* Update code: 02/17/2011 by Thangnc */
            try
            {
                string sVerb = (pFormMode == FormModes.NewMode) ? AulacEmsVerb.MngStore.TranStore.AddVchTranStore : AulacEmsVerb.MngStore.TranStore.UpdateVchTranStore;
                XmlDataDocument xmlDoc = CreateXmlDocument(sVerb, AulacEmsVerb.MngStore.TranStore.Noun.TranStore);

                XmlElement eHeader = XmlProcess.AddVchHeaderElm(ref xmlDoc);
                foreach (Aulac.Client.Global.Interface.IAulacValueDataCtrl ctr in GetListControlsOnForm())
                    ctr.GetValueToXml(ref eHeader);

                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.TagStore, TagStores.InputStore);
                XmlProcess.SetAtrb(ref eHeader, tagGeneral.VchTypeID, mVchType);

                // Noi giao
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.ObjTypeRelsID, cboObjRels.ComboObjectTyeps.ObjTypeID);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.ObjRelsPrkID, cboObjRels.ObjPrkID);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.ObjRelsName, cboObjRels.ObjName);
                // Noi nhan
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.ObjTypeRecID, cboObjRec.ComboObjectTyeps.ObjTypeID);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.ObjRecPrkID, cboObjRec.ObjPrkID);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.ObjRecName, cboObjRec.ObjName);
                XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.IsCheckQtyLost, mIsCheckInstock);

                if (pFormMode == FormModes.EditMode)
                    XmlProcess.SetAtrb(ref eHeader, tagVch.Inv.VchTranInvPrkID, mVoucherID);
                XmlElement eLines = XmlProcess.AddVchLinesElm(ref xmlDoc);
                grdLine.SetValueInColumn(tagVch.Inv.StorePrkID, cboRelsStore.StorePrkID);
                ((IAulacDataContainerCtrl)grdLine).GetDataToChildNodeXml(eLines);
                xmlData = DataMngStoreHelper.SaveDataToServer(xmlDoc);
                return true;                
            }
            catch (Exception ex)
            {
                Aulac.Global.Exceptions.AulacException x = new Aulac.Global.Exceptions.AulacException(ex);
                if (ErrCodes.ErrorCode.Error0010.Equals(x.ErrorCode))
                {
                    XmlNodeList lst = XmlProcess.GetListVchLineItem(x.XmlEcexption);
                    foreach (XmlElement eLine in lst)
                    {
                        DataRow row = grdLine.AulacDataGrid.GetFirstDataRowByKey(tagVch.Inv.ItemPrkID, XmlProcess.GetValueAtrb(eLine, tagVch.Inv.ItemPrkID));
                        DataTableProcess.SetValueDataRow(row, tagVch.Inv.OverInstock, true);
                    }
                    return false;
                }
                else throw ex;
            }            
        }
        private string GetNewVchNo(ref string pVchName)
        {
            XmlDataDocument xmlDoc = DataRequest.CreateXmlDocument(Aulac.Verb.Ems.AulacEmsVerb.MngStore.RecStore.GetNewVchNo, Aulac.Verb.Ems.AulacEmsVerb.MngStore.RecStore.Noun.RecvStore);
            XmlProcess.AddValueParamItem(xmlDoc, tagVch.Inv.VchTypeID, mVchType);
            XmlDataDocument outDoc = DataRequest.ProcessRequest(xmlDoc);
            pVchName = XmlProcess.GetValueVchHeader(outDoc, tagVch.Inv.VchName);
            return XmlProcess.GetValueVchHeader(outDoc, tagVch.Inv.VchNo);
        }
    }
}
